package com.test.pm;

import java.awt.*;
import java.net.MalformedURLException;

import javax.swing.*;

import com.rosiminc.pm.game.PipesManiaGame;
import com.rosiminc.pm.game.Tile;


public class PMTestFrame02 extends JFrame {

	private static final long serialVersionUID = 1L;
	private final int WIDTH = 10;
	private final int HEIGHT = 5;
	
	private Tile[][] board;


	public PMTestFrame02(){
		super();
		
		initializeComponents();
		setVisible(true);
		pack();
		//setSize(new Dimension(400,400));
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	private void initializeComponents(){
		
		PipesManiaGame game = new PipesManiaGame(HEIGHT, WIDTH);
		board = game.getBoard();
		
		this.add(getScrollPane(), BorderLayout.CENTER);
		/*try {
			//this.add(getImagePane(), BorderLayout.CENTER);
		} catch (MalformedURLException e) {
			// T ODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
	
	
	private JPanel getImagePane() throws MalformedURLException {
		JPanel pane = new JPanel();
		JLabel label = new JLabel(new ImageIcon("img/p0.bmp"));
		pane.add(label);
		return pane;
	}

	private JPanel getScrollPane() {
		JPanel scrollPanel = new JPanel(new BorderLayout());
		
		JScrollPane scroll = new JScrollPane(getMainGridPanel(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPanel.add(scroll, BorderLayout.CENTER);
		
		return scrollPanel;
	}
	//X XX sid = 2009130904

	private JPanel getMainGridPanel() {
		JPanel panel = new JPanel(new GridLayout(HEIGHT,WIDTH,0,0));
		
		for(int row=0; row < HEIGHT; row++)
			for(int col=0; col < WIDTH; col++)
			{
				panel.add(getTile(row, col));
			}
		
		JPanel flowPanel = new JPanel();//FlowLayout
		flowPanel.add(panel);
		
		/*System.out.println(MediaTracker.ABORTED);
		System.out.println(MediaTracker.COMPLETE);
		System.out.println(MediaTracker.ERRORED);
		System.out.println(MediaTracker.LOADING);
		System.out.println();*/
		
		return flowPanel;
	}

	private Component getTile(int row, int col) {
		
		JPanel panel = new JPanel();
		
		Tile t = board[row][col];
		
		
		panel.setBorder(
				BorderFactory.createCompoundBorder(
						BorderFactory.createEmptyBorder(
								0,0,0,0),
				
				
				BorderFactory.createMatteBorder(
						t.isUp()?0:3, t.isLeft()?0:3,
						t.isDown()?0:3, t.isRight()?0:3,
						(row+col)%2==0?Color.BLUE:Color.RED)));
		
		/*int tileNum = (int)(Math.random()*16);
		String address = "img/p" + tileNum + ".bmp";
		ImageIcon image = new ImageIcon(address);
		System.out.println(address);
		JLabel button = new JLabel(image);
		
		//button.setIcon(image);
		
		return button;*/
		panel.add(new JButton("X"));
		
		return panel;
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable(){
			public void run()
			{
				new PMTestFrame02();
			}
		});
	}

}
